package Junit.Server;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.Socket;

import junit.framework.TestCase;
import server.WSEPHandler;
import acceptanceTests.registration.RegistrationInt;
import acceptanceTests.registration.RegistrationProxy;


public class WSEPHandlerTest extends TestCase {
	WSEPHandler handler;
	RegistrationInt _forumRegimp = new RegistrationProxy();
	
	public WSEPHandlerTest() {
		try{
			Socket s= new Socket("localhost", getPort());
			
			handler= new WSEPHandler(s,_forumRegimp.getServer());
		}
		catch(IOException e){
		}
	}
	
	public void testHandler(){
		assertTrue(handler.registerUser("user81","abc@gmail.com","password1").equals("true"));
		assertTrue((handler.login("user81", "password1")).startsWith("MEMBER"));
		assertEquals(handler.promoteUser("user81"), "true");
		assertEquals(handler.demoteUser("user81"), "true");
		
		assertFalse(handler.getRootTopic().equals(""));
		assertTrue(handler.setCurrentlyViewedItem(_forumRegimp.getServer().getDataRepository().getRootTopic()));
		assertTrue(handler.getItem(1)!="");
		assertTrue(handler.addThread("subject3", "text2", "user81").equals("true"));
		assertEquals(handler.logout(), "true");
		_forumRegimp.deleteRegisterUser("user81");


	}
	
	
	
	
	private static int getPort() {
		String str="0";
		try {
	        BufferedReader in = new BufferedReader(new FileReader("port.txt"));
	        str = in.readLine();
	        in.close();
	    } catch (IOException e) {
	    }
		return Integer.parseInt(str);
	}
		
	

}
